package user

import (
	"context"

	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/userServiceClient/userrpc"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserCurrencyExpRateConfigListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserCurrencyExpRateConfigListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserCurrencyExpRateConfigListLogic {
	return &UserCurrencyExpRateConfigListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserCurrencyExpRateConfigListLogic) UserCurrencyExpRateConfigList() (resp *types.CurrencyExpExchangeConfigListResponse, err error) {
	rpcResp, err := l.svcCtx.UserServiceRPC.UserGetServiceConfigByType(l.ctx, &userrpc.GetServiceConfigRequest{ConfigType: userrpc.ConfigType_COIN_EXPERIENCE})
	if err != nil {
		logc.Error(l.ctx, "UserCurrencyExpRateConfigListLogic.UserCurrencyExpRateConfigList", err)
		return &types.CurrencyExpExchangeConfigListResponse{
			Configs: []*types.CurrencyExpExchangeConfig{},
		}, nil
	}
	resp = &types.CurrencyExpExchangeConfigListResponse{}
	configList := make([]*types.CurrencyExpExchangeConfig, len(rpcResp.Configs))
	for i, config := range rpcResp.Configs {
		configList[i] = &types.CurrencyExpExchangeConfig{
			ConfigId:     config.Id,
			CurrencyName: config.ConfigName,
			ExchangeRate: config.ConfigValue,
		}
	}
	resp.Configs = configList
	return
}
